package com.lottoad.android.cronos.c2dm;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.lottoad.android.util.ApplicationUtils;
import com.lottoad.android.util.LottoAdConstants;

/**-
 * C2DM Receiver
 */
public class C2DMReceiver extends BroadcastReceiver {
	NotificationManager mNotiMgr;

    @Override
    public void onReceive(Context context, Intent intent) {
    	
	    if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
	    {
		    handleRegistration(context, intent);
		    Log.d("onReceive","onReceive1");
	    }
	    else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE"))
	    {
	    	handleMessage(context, intent);
	    	Log.d("onReceive","onReceive2");
	    }
    }
    
    private void handleMessage(Context context, Intent intent) 
    {
    	 Log.e("###############", "handleMessage");
    	 
         String title = intent.getStringExtra("title"); 
         String msg = intent.getStringExtra("msg");
         String type = intent.getStringExtra("type");
         String sendSeq = null;
         
         if(type.equals("0"))
        	 sendSeq = intent.getStringExtra("sendSeq");
         
         Log.d("handleMessage","title ==============>" + title);
         Log.d("handleMessage","msg ==============>" + msg);
         Log.d("handleMessage","type ==============>" + type);
         Log.d("handleMessage","sendSeq ==============>" + sendSeq);
         
         PushWakeLock.acquireCpuWakeLock(context);
  
         Intent i = new Intent(context, ShowMsg.class);
         Bundle b = new Bundle();
         b.putString("title", title);
         b.putString("msg", msg);
         b.putString("type", type);
         if(sendSeq != null && sendSeq.length() > 0){
        	 b.putString("sendSeq", sendSeq);
         }
         i.putExtras(b);
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(i);
    }
    
    private void handleRegistration(Context context, Intent intent) 
	{
    	String mRegistrationId = intent.getStringExtra("registration_id");
    	Log.d("BenTag","mRegistrationId : "+mRegistrationId);
		if (intent.getStringExtra("error") != null){
			
			String error = intent.getStringExtra("error");
			if (error == "SERVICE_NOT_AVAILABLE") 
			{
			} else if (error == "ACCOUNT_MISSING") 
			{
			} else if (error == "AUTHENTICATION_FAILED") 
			{
			} else if (error == "TOO_MANY_REGISTRATIONS") 
			{
			} else if (error == "INVALID_SENDER") 
			{
			} else if (error == "PHONE_REGISTRATION_ERROR") 
			{
			}
		} 
		else if (intent.getStringExtra("unregistered") != null){
		}else if (mRegistrationId != null){
						
			ApplicationUtils.setStrPreferences(context, LottoAdConstants.PREFER_REGISTRATION_ID, mRegistrationId);
			
		}
	}
}